//
//  StrategyInspectResult.m
//  StrategySimulator
//
//  Created by jiro on 12/07/05.
//  Copyright 2012 jiro music. All rights reserved.
//

#import "StrategyRawPerfomance.h"

#import "../Commons.h"
#include <algorithm>
 
void StrategyRawPerfomance::addResult( const StrategyRawPerfomance& r )
{
	copy( r.investMoneies.begin(), r.investMoneies.end(), std::back_inserter( investMoneies ) );
	copy( r.profits.begin(), r.profits.end(), std::back_inserter( profits ) );
	copy( r.tradeTimeIntervals.begin(), r.tradeTimeIntervals.end(), std::back_inserter( tradeTimeIntervals ) );
}

void StrategyRawPerfomance::addInvestMoney( double investMoney, double profit, double interval )
{
	investMoneies.push_back( investMoney );
	profits.push_back( profit );
	tradeTimeIntervals.push_back( interval );
}

StrategyPerfomance StrategyRawPerfomance::getStrategyPerfomance() const
{
	StrategyPerfomance r;
	r.parameterIndex = parameterIndex;
	r.tradeCount = getTradeCount();
	r.winRate = getWinRate();
	r.interestRate = getInterestRate();
	r.profitPerTrade = getProfitPerTrade();
	r.dayIntervalPerTradeDay = getDayIntervalPerTrade();
	return r; 
}

int StrategyRawPerfomance::getParameterIndex() const
{
	return parameterIndex;
}

int StrategyRawPerfomance::getTradeCount() const
{
	return investMoneies.size();
}

int StrategyRawPerfomance::getWinCount() const
{
	int c = 0;
	for ( int i = 0, count = getTradeCount(); i < count; i++ )
	{
		if ( profits[ i ] > 0 )
		{
			++c;
		}
	}
	return c;

}

double StrategyRawPerfomance::getWinRate() const
{
	return (double) getWinCount() / getTradeCount();
}

double StrategyRawPerfomance::getInterestRate() const
{
	double profitSum = 0.0;
	double investmentMoneySum = 0.0;
	for ( int i = 0, count = getTradeCount(); i < count; i++ )
	{
		investmentMoneySum += investMoneies[ i ];
		profitSum += profits[ i ];
	}
	return profitSum / investmentMoneySum;
}

double StrategyRawPerfomance::getProfitPerTrade() const
{
	double profitSum = 0.0;
	for ( int i = 0, count = getTradeCount(); i < count; i++ )
	{
		profitSum += profits[ i ];
	}
	return profitSum / getTradeCount();
}

double StrategyRawPerfomance::getDayIntervalPerTrade() const
{
	double sum = 0.0;
	for ( int i = 0, count = getTradeCount(); i < count; i++ )
	{
		sum += tradeTimeIntervals[ i ] / secondsForOneDay;
	}
	return sum / getTradeCount();
}

NSString* StrategyRawPerfomance::toString() const
{
	NSString* s = [ NSString stringWithFormat: 
		@"tradeCount:%d, winRate: %f, interestRate: %f, profitPerTrade: %f, timeIntervalPerTrade(days): %f",
		getTradeCount(), getWinRate(), getInterestRate(), getProfitPerTrade(), getDayIntervalPerTrade() ];
	return s;
}
